package com.niit.notification.controller;

import com.niit.notification.entity.Notification;
import com.niit.notification.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 */
@RestController
@RequestMapping("/api")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 获取所有通知（点赞、评论、关注）
     */
    @GetMapping("/notifications/{userId}")
    public ResponseEntity<?> getAllNotifications(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        try {
            List<Map<String, Object>> notifications = notificationService.getAllNotifications(userId, page, size);
            return ResponseEntity.ok(notifications);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取通知失败: " + e.getMessage());
        }
    }

    /**
     * 获取点赞通知
     */
    @GetMapping("/like-notifications")
    public ResponseEntity<?> getLikeNotifications(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        try {
            List<Map<String, Object>> notifications = notificationService.getLikeNotifications(userId, page, size);
            return ResponseEntity.ok(notifications);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取点赞通知失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取用户点赞通知（使用实体类）
     */
    @GetMapping("/like/entity")
    public ResponseEntity<?> getLikeNotificationsEntity(
            @RequestParam("userId") Long userId,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        try {
            List<Notification> notifications = notificationService.getLikeNotificationsEntity(userId, page, size);
            return ResponseEntity.ok(notifications);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取点赞通知实体失败: " + e.getMessage());
        }
    }

    /**
     * 获取评论通知
     */
    @GetMapping("/comment-notifications")
    public ResponseEntity<?> getCommentNotifications(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        try {
            List<Map<String, Object>> notifications = notificationService.getCommentNotifications(userId, page, size);
            return ResponseEntity.ok(notifications);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取评论通知失败: " + e.getMessage());
        }
    }

    /**
     * 获取关注通知
     */
    @GetMapping("/follow-notifications")
    public ResponseEntity<?> getFollowNotifications(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        try {
            List<Map<String, Object>> notifications = notificationService.getFollowNotifications(userId, page, size);
            return ResponseEntity.ok(notifications);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取关注通知失败: " + e.getMessage());
        }
    }

    /**
     * 获取通知总数
     */
    @GetMapping("/notifications/{userId}/count")
    public ResponseEntity<?> getNotificationCount(@PathVariable Long userId) {
        try {
            int count = notificationService.getNotificationCount(userId);
            Map<String, Integer> result = new HashMap<>();
            result.put("count", count);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取通知数量失败: " + e.getMessage());
        }
    }

}